package cn.com.nou.base.controller.bdm;

import cn.com.nou.base.dao.bdm.SchoolSystemMapper;
import cn.com.nou.base.service.bdm.SchoolSystemService;
import cn.com.nou.common.BaseController;
import cn.com.nou.common.BaseResponse;
import cn.com.nou.common.BaseService;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.NouUserInfoT;
import cn.com.nou.common.entity.Pagination;
import cn.com.nou.common.utils.Utils;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.OffsetDateTime;
import java.util.List;

@RestController
@RequestMapping(path="/schoolSystem")
public class SchoolSystemController {

    @Autowired
    SchoolSystemService schoolSystemService;

    @GetMapping(path = "/list")
    public ResponseEntity getSchoolSystemList(NouEnumerationT nouEnumerationT, int pageNo, int pageSize) {
        Pagination schoolSystemList = schoolSystemService.getSchoolSystemlist(nouEnumerationT,pageNo, pageSize);
        return BaseResponse.generateOKResponseEntity(schoolSystemList);
    }

    @PostMapping(path = "/add")
    public ResponseEntity addSchoolSystem(NouEnumerationT nouEnumerationT) {
        int code = schoolSystemService.addSchoolSystem(nouEnumerationT);
        if (code == 1){
            return BaseResponse.generateOKResponseEntity("200","添加成功");
        }else if (code == 2){
            return BaseResponse.generateBadResponseEntity(500,"添加失败","存在相同学制名称不可添加");
        } else {
            return BaseResponse.generateBadResponseEntity(500,"添加失败","");
        }
    }

    @PostMapping(path = "/update")
    public ResponseEntity updateSchoolSystem(NouEnumerationT nouEnumerationT) {
        int code1 = schoolSystemService.updateSchoolSystem(nouEnumerationT);
        if (code1 == 1){
            return BaseResponse.generateOKResponseEntity("200","更新成功");
        }else if (code1 == 2){
            return BaseResponse.generateBadResponseEntity(500,"更新失败","存在相同学制名称不可更新");
        } else {
            return BaseResponse.generateBadResponseEntity(500,"更新失败","");
        }
    }

    @PostMapping(path = "/delete")
    public ResponseEntity deleteSchoolSystem(NouEnumerationT nouEnumerationT) {
        List<Integer> codes = schoolSystemService.deleteSchoolSystem(nouEnumerationT);
        StringBuffer bdmMessage = new StringBuffer();
        if (codes.contains(3)) {
            return BaseResponse.generateOKResponseEntity("200","删除成功");
        }
        if (codes.contains(1)) {
            bdmMessage.append("层次被学生使用,");
        }

        if (codes.contains(2)) {
            bdmMessage.append("层次被院校使用,");
        }
        return BaseResponse.generateBadResponseEntity(500, "层次被使用", bdmMessage.toString());
    }
}
